<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@include file="/common/session.jsp" %>
<c:set var="userId" value="${param.userId}"/>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=2.0, user-scalable=no, width=device-width">
    <LINK type="text/css" rel="stylesheet" href="${ctx}/css/default.css"/>
    <script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/util.js"></script>
    <script type='text/javascript' src='${ctx}/dwr/interface/MarketRpcService.js'></script>
    <script type='text/javascript' src='${ctx}/dwr/interface/UserRpcService.js'></script>
    <script type="text/javascript" src="${ctx}/js/loading.js"></script>
</head>
<body>
<form id="userForm">
    <center>
        <input type="hidden" id="userId" name="userId" value="${userId}">
        <input type="hidden" id="userState" name="userState" value="1">
        <input type="hidden" id="accountId" name="accountId">
        <table width="100%" cellpadding="5" cellspacing="5">
            <tr>
                <td>
                    所属市场:
                </td>
                <td>
                    <select id="marketId" name="marketId">
                    </select>
                </td>
                <td>
                    所属角色:
                </td>
                <td>
                    <select id="userType" name="userType">
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    用户电话:
                </td>
                <td>
                    <input type="tel" placeholder="用户电话(登录名,必填)" id="userMobile" name="userMobile"  onchange="checkMobile()"   maxlength="11" required/>
                </td>
                <td>
                    用户姓名:
                </td>
                <td>
                    <input type="text" placeholder="用户姓名(必填)" id="userName" name="userName" maxlength="10" required/>
                </td>
            </tr>
            <tr>
                <td style="border-bottom: 1px solid #e1ecf5" colspan="6">
                </td>
            </tr>
            <tr>
                <td colspan="6" align="center">
                    <button type="button" onclick="insertOrUpdateUser()">保存</button>
                    <button type="button" onclick="callbackUser()">返回</button>
                </td>
            </tr>
        </table>
    </center>
</form>
</body>
</html>

<script>
    //    显示市场列表
    var market = new Market();
    market.marketId = "${marketId}";
    MarketRpcService.selectMarketList(market, callbackSelectMarketList);

    function callbackSelectMarketList(list) {
        dwr.util.addOptions("marketId", list, "marketId", "marketName");
        dwr.util.setValue("marketId", "${marketId}");
        if ("${userId}".length > 0) {
            selectUserList();
            $("userType").disabled = true;
        }
    }

    roleList();
    function roleList(){
    	var check_hash = {
                "10":"总公司管理员",
                "11":"总公司审核员",
                "30":"市场管理员",
                "31":"市场员工",
                "32":"市场检测员",
                "40":"车商",
                "41":"车商员工"
            };
            var check_hash_market = {
                "32":"市场检测员",
                "30":"市场管理员",
                "31":"市场员工"
            };
            var check_hash_market1 = {
                "30":"市场管理员",
	            "31":"市场员工",
	            "32":"市场检测员",
	            "40":"车商",
	            "41":"车商员工"
            };
            var check_hash_shop = {
                "40":"车商",
                "41":"车商员工"
            };

            if ("${userId}".length == 0){
                dwr.util.addOptions("userType", check_hash_market);
            }else{
                if('${marketId}' == "1"){
                    dwr.util.addOptions("userType", check_hash);
                }else if('${shopId}' == ""){
                    dwr.util.addOptions("userType", check_hash_market1);
                }else{
                    dwr.util.addOptions("userType", check_hash_shop);
                }
            }

    }

    function selectUserList() {
        var user = new User();
        user.userId = "${userId}";
        UserRpcService.selectUserList(user, callbackSelectUserList);
    }

    function callbackSelectUserList(list) {
        dwr.util.setValues(list[0]);
    }

    function checkMobile(){
        var user = new User();
        user.userState = 1;
        user.userMobile = dwr.util.getValue("userMobile");
        UserRpcService.selectUserList(user,callbackSelectMobileList);
        function callbackSelectMobileList(list){
            if (list.length > 0 ){
                dwr.util.setValue("userMobile",'');
                alert("手机号重复请重填");
            }
        }
    }

    function insertOrUpdateUser() {
        if (!validate($("userForm"))) {
            return false;
        }
        var user = dwr.util.getValues("userForm");
        user.userRemark = dwr.util.getText("userType");
        if ("${userId}".length > 0) {
            UserRpcService.updateUser(user, callbackUser);
        } else {
            UserRpcService.insertUser(user, callbackUser);
        }
    }

    function callbackUser(user) {
        location.href = "${ctx}/user/user_list.jsp";
    }

</script>
